var threeSumClosest = function (nums, target) {
  nums.sort((a, b)  => a - b);
  let len = nums.length;
  let ans = nums[0] + nums[1] + nums[2];
  for (let i = 0; i < len; i++) {
    let L = i + 1;
    let R = len - 1;
    while (L < R) {
      let sum = nums[i] + nums[L] + nums[R];
      if (Math.abs(sum - target) < Math.abs(ans - target)) {
        ans = sum;
      }
      if (sum < target) {
        L++
      } else if (sum > target) {
        R--
      } else {
        return ans
      }
    }
  }
  return ans;
}